﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormOrderList : FormTemplet
    {
        private OrderCondition _orderCondition = null;

        public FormOrderList()
        {
            this.InitializeComponent();
        }

        public FormOrderList(OrderCondition oc) : this()
        {
            this._orderCondition = oc;
            this.LoadOrdersByCondition();
        }

        private void LoadOrdersByCondition()
        {
            DataTable dataTable = null;
            Orders orders = new Orders();
            try
            {
                dataTable = orders.GetOrdersByCondition(this._orderCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n订单信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            orders.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    this.dgOrders.DataSource = dataTable;
                    this.dgOrders.AutoResizeColumns();
                }
                else
                {
                    MessageBox.Show("根据你输入的条件，在系统中没有找到你要得信息", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgOrders.DataSource = null;
                    base.Dispose();
                }
                dataTable.Dispose();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Update])
            {
                MessageBox.Show("你没有权限操作这里！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                try
                {
                    string text = this.dgOrders.Rows[0].Cells[0].Value.ToString();
                }
                catch (Exception)
                {
                    MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                int rowIndex = this.dgOrders.CurrentCell.RowIndex;
                FormUpdateOrder formUpdateOrder = new FormUpdateOrder(this.dgOrders.Rows[rowIndex].Cells[1].Value.ToString());
                formUpdateOrder.MdiParent = base.MdiParent;
                formUpdateOrder.Show();
                formUpdateOrder.Activate();
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dgOrders.Rows[0].Cells[0].Value.ToString();
            }
            catch (Exception)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dgOrders.CurrentCell.RowIndex;
            new FormOrderInfo(this.dgOrders.Rows[rowIndex].Cells[1].Value.ToString())
            {
                MdiParent = base.MdiParent
            }.Show();
        }

        private void dgOrders_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex <= this.dgOrders.Rows.Count - 1)
            {
                new FormOrderInfo(this.dgOrders.Rows[e.RowIndex].Cells[1].Value.ToString())
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void FormOrderList_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dgOrders.Rows.Count; i++)
            {
                if (Convert.ToInt32(this.dgOrders.Rows[i].Cells["订单生成次数"].Value) > 0)
                {
                    this.dgOrders.Rows[i].Cells["订单生成次数"].Style.BackColor = Color.Red;
                }
                if (Convert.ToInt32(this.dgOrders.Rows[i].Cells["标签打印次数"].Value) > 0)
                {
                    this.dgOrders.Rows[i].Cells["标签打印次数"].Style.BackColor = Color.Red;
                }
                if (this.dgOrders.Rows[i].Cells["附件"].Value.ToString().Length > 0)
                {
                    this.dgOrders.Rows[i].Cells["附件"].Style.BackColor = Color.Red;
                }
            }
            this.dgOrders.AutoResizeColumns();
        }

        private void dgOrders_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            for (int i = 0; i < this.dgOrders.Rows.Count; i++)
            {
                if (Convert.ToInt32(this.dgOrders.Rows[i].Cells["订单生成次数"].Value) > 0)
                {
                    this.dgOrders.Rows[i].Cells["订单生成次数"].Style.BackColor = Color.Red;
                }
                if (Convert.ToInt32(this.dgOrders.Rows[i].Cells["标签打印次数"].Value) > 0)
                {
                    this.dgOrders.Rows[i].Cells["标签打印次数"].Style.BackColor = Color.Red;
                }
                if (this.dgOrders.Rows[i].Cells["附件"].Value.ToString().Length > 0)
                {
                    this.dgOrders.Rows[i].Cells["附件"].Style.BackColor = Color.Red;
                }
            }
            this.dgOrders.AutoResizeColumns();
        }
    }
}
